package ctxSemanticsGen;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class LayerWrapperManagerGenerator {
	
	
	private ArrayList<Class> layers;
	
	
	public LayerWrapperManagerGenerator(ArrayList<Class> layers){
		this.layers = layers;
	}
	
	
	
	public void generateWrapper(String path){
		
		// path + "/"
		run(path + Constants.FILE_SEPARATOR);
	}
		
	private void run(String path){
		
		VelocityContext context = new VelocityContext();
        
        ArrayList<String[]> layersWithBase = new ArrayList<String[]>();
        ArrayList<String[]> layersWithoutBase  = new ArrayList<String[]>();
        for(Class l : layers){
        	
        	// Get the layerWrapper name for the layer
        	String layerWrapperName = Utility.buildUniqueLayerWrapperName(l.getName());
        	
        	String layerName = l.getName();
            
            String[] s = {layerName,layerWrapperName};
            layersWithBase.add(s);
            layersWithoutBase.add(s);
        }
        
        String baseWrapperName = Utility.buildUniqueLayerWrapperName(Utility.BASE_LAYER_NAME);
        String layerName = Utility.BASE_LAYER_NAME;
        
        String[] s = {layerName,baseWrapperName};
        layersWithBase.add(s);
        
        context.put("layersWithBase",layersWithBase);
        context.put("layersWithoutBase",layersWithoutBase);
        

        /* render the template TODO: Add the encoding */
        StringWriter w = new StringWriter();
        Velocity.mergeTemplate("layerWrapperManagerTemplate.vm", context, w );
 
        
        /* Generate the file */
        try {
        	BufferedWriter out = new BufferedWriter(new FileWriter(path + "LayerWrapperManager" + ".java"));
        	out.write(w.toString());
        	out.close();
        }catch (IOException e){
        	System.out.print("Error writing generated aspect to file!");
        	System.exit(1);
        }

		
	}

}


